/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewTable.java
 *
 * Created on 2009-01-06, 19:59:29
 */

package browser;

import DBBrowser_pack.DBLogin;

/**
 *
 * @author Dom
 */
public class NewTable extends javax.swing.JFrame {

    /** Creates new form NewTable */
    public NewTable() {
   
        initComponents();
   tabelka();
   jTable1.repaint();    
        
    }
    public void cos(){
               java.util.Vector<String> s=new java.util.Vector();
         s=DBLogin.getManager().xxx();
        for(int i=0;i<s.size();i++)
         boxmodel1.addElement(s.get(i));  
    }
public void tabelka(){
    
    jTable1.setModel(model);  
    boxmodel2.addElement(" ");
    boxmodel2.addElement("null");
        boxmodel2.addElement("not null");
        boxmodel3.addElement(" "); 
        boxmodel3.addElement("primary key");
        boxmodel3.addElement("foreign key");  
        box1.setModel(boxmodel1); 
        box2.setModel(boxmodel2);
          box3.setModel(boxmodel3);
    model.addColumn("Nazwa");
        //model.addColumn(colmodel1);
        model.addColumn("Typ danych");
        model.addColumn("null/not null");
        model.addColumn("PK/FK");
        model.addColumn("Długość");
      model.addRow(new String[]{""," "," "," ",""});  
        cos();
jTable1.repaint();
        javax.swing.table.TableColumn col1 = jTable1.getColumnModel().getColumn(1);
        col1.setCellEditor(new javax.swing.DefaultCellEditor(box1));

        javax.swing.table.TableColumn col2 = jTable1.getColumnModel().getColumn(2);
        col2.setCellEditor(new javax.swing.DefaultCellEditor(box2));

        javax.swing.table.TableColumn col3 = jTable1.getColumnModel().getColumn(3);
        col3.setCellEditor(new javax.swing.DefaultCellEditor(box3));

}
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Nazwa", "Typ Danych", "Null/Not Null", "PK / FK", "Opcje"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

        jButton1.setText("Wykonaj");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Nazwa");

        jLabel2.setText("Kolumny");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyPressed
//javax.swing.JOptionPane.showMessageDialog(rootPane, evt.getKeyCode());
          if(evt.getKeyCode()==10)
            if(jTable1.getSelectedRow()==jTable1.getRowCount()-1)
                if(jTable1.getSelectedColumn()==jTable1.getColumnCount()-1)
                    if(model.getValueAt(model.getRowCount()-1,0).toString().isEmpty()==false)
              
        model.addRow(new String[]{""," "," "," ",""});
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1KeyPressed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
String s="",z="",x="";
for(int i=0;i<model.getRowCount();i++){
    x=model.getValueAt(i, 0).toString()+" "+model.getValueAt(i, 1).toString()+" "+model.getValueAt(i, 2).toString()+model.getValueAt(i, 3).toString();
if(i<model.getRowCount()-1&&model.getValueAt(i,0).toString().isEmpty()==false)
    z=z+x+", ";
else z=z+x;
}
//if(boxmodel3.getSelectedItem().equals(x))
s=jTextField1.getText()+" ("+z+" );";
jTextField2.setText(s);
DBLogin.getManager().createTable(s);

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewTable().setVisible(true);
            }
        });
    }
    javax.swing.JComboBox box1=new javax.swing.JComboBox();
    javax.swing.JComboBox box2=new javax.swing.JComboBox();
    javax.swing.JComboBox box3=new javax.swing.JComboBox();
    javax.swing.JComboBox box4=new javax.swing.JComboBox();
    javax.swing.table.DefaultTableColumnModel colmodel1=new javax.swing.table.DefaultTableColumnModel();
    javax.swing.table.DefaultTableColumnModel colmodel2=new javax.swing.table.DefaultTableColumnModel();
    javax.swing.table.DefaultTableColumnModel colmodel3=new javax.swing.table.DefaultTableColumnModel();
    javax.swing.table.DefaultTableColumnModel colmodel4=new javax.swing.table.DefaultTableColumnModel();
    javax.swing.DefaultComboBoxModel boxmodel1=new javax.swing.DefaultComboBoxModel();
    javax.swing.DefaultComboBoxModel boxmodel2=new javax.swing.DefaultComboBoxModel();
    javax.swing.DefaultComboBoxModel boxmodel3=new javax.swing.DefaultComboBoxModel();
    javax.swing.DefaultComboBoxModel boxmodel4=new javax.swing.DefaultComboBoxModel();
    protected javax.swing.table.DefaultTableModel model= new javax.swing.table.DefaultTableModel();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

}
